package com.example.object.game;

import android.content.Context;
import android.util.Log;

import com.example.bounce.R;
import com.example.globject.GLSquare;
import com.example.object.GameShape;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

/**
 * Created by Lucas on 06/07/13.
 */
public class Anchor extends GameShape {

    private GLSquare _glSquare;
    private BodyDef _bodyDef;
    private Body _body;

    private float _size;
    private final String TAG = "Anchor";

    public Anchor(Context context, float xCenter, float yCenter, float size){
        Log.i(TAG, "constructor");
        _handlerInitiate = false;
        // doesn't need to be initialized
        _userData = 0;

        _size = size;
        _glSquare = new GLSquare(context, xCenter, yCenter, size, size, R.raw.anchor);

        // create definition
        _bodyDef = new BodyDef();

        _bodyDef.position.set(new Vec2(xCenter, yCenter));
        _bodyDef.type = BodyType.STATIC;
    }

    public void addToWorld(World world){
        // step 2 : create the body;
        _body = world.createBody(_bodyDef);
        // step 3 : create the fixture;
        // need to reverse the width and height for some reason
        CircleShape shape = new CircleShape();
        shape.m_p.set(new Vec2(_size, _size));
        shape.m_radius = _size;
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        //fd.friction = 0f;
        fd.restitution = 0.5f;
        _body.createFixture(fd);
    }

    public void setGLHandler(int positionHandle, int textureHandle, int MVPMatrixHandle,
                             float vMatrix[], float projMatrix[]){
        _glSquare.setGLHandler(positionHandle, textureHandle, MVPMatrixHandle, vMatrix, projMatrix, null);
        _handlerInitiate = true;
    }

    @Override
    public void draw(){
        _glSquare.draw();
    }

    public String toString(){
        return "\n glSquare: " + _glSquare.toString();
    }

    public GLSquare getGLSquare(){
        return _glSquare;
    }

    @Override
    public Body getBody() {
        return _body;
    }
}
